package org.song.videoplayer;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class DialogHelper {
    private static DialogHelper dialogHelper;
    private CommonDialog commonDialog;
    private final Component component;

    private DialogHelper(Context context) {
        commonDialog = new CommonDialog(context);
        component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_user_common_dialog_layout, null, true);
        commonDialog.setContentCustomComponent(component);
    }

    public static DialogHelper CreateDialog(Context context) {
        dialogHelper = new DialogHelper(context);
        return dialogHelper;
    }

    public DialogHelper setDialogSize(int width, int height) {
        commonDialog.setSize(width, height);
        return dialogHelper;
    }

    public DialogHelper setTitle(String title) {
        Text text = (Text) component.findComponentById(ResourceTable.Id_title);
        text.setVisibility(Component.VISIBLE);
        text.setText(title);
        return dialogHelper;
    }

    public DialogHelper setContentText(String content) {
        Text text = (Text) component.findComponentById(ResourceTable.Id_content);
        text.setText(content);
        return dialogHelper;
    }

    public DialogHelper setPositiveButton(String text, OnDialogBtnClicked listener) {
        Button btnLeft = (Button) component.findComponentById(ResourceTable.Id_btn_left);
        btnLeft.setVisibility(Component.VISIBLE);
        btnLeft.setText(text);
        btnLeft.setClickedListener(component -> listener.onClicked(commonDialog));
        return dialogHelper;
    }

    public DialogHelper setNegativeButton(String text, OnDialogBtnClicked listener) {
        Button btnRight = (Button) component.findComponentById(ResourceTable.Id_btn_right);
        btnRight.setVisibility(Component.VISIBLE);
        btnRight.setText(text);
        btnRight.setClickedListener(component -> listener.onClicked(commonDialog));
        return dialogHelper;
    }

    public void showDialog() {
        commonDialog.show();
    }

    public void dismissDialog() {
        commonDialog.hide();
        commonDialog.destroy();
    }

    public interface OnDialogBtnClicked {
        void onClicked(CommonDialog commonDialog);
    }
}
